import java.util.Scanner;

public class demo05 {
    public static int n=30;
    public static int m=50;
    public static int count=0;
    public static int min=99999999;
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int[][] arr=new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j]=sc.nextInt();
            }
        }
        arr[0][0]=2;
        dfs(arr,0,0);
        System.out.println(min);
    }
    public static void dfs(int[][] arr,int x,int y){
        if((x==n-1)&&(y==m-1)){
            if(min>count){
                min=count;
                return;
            }
        }
        if((y+1<m)&&(arr[x][y+1]==0)){
            arr[x][y+1]=2;
            count++;
            dfs(arr,x,y+1);
            arr[x][y+1]=0;
            count--;
        }
        if((x+1<n)&&(arr[x+1][y]==0)){
            arr[x+1][y]=2;
            count++;
            dfs(arr,x+1,y);
            arr[x+1][y]=0;
            count--;
        }
        if((y-1>=0)&&(arr[x][y-1]==0)){
            arr[x][y-1]=2;
            count++;
            dfs(arr,x,y-1);
            arr[x][y-1]=0;
            count--;
        }
        if((x-1>=0)&&(arr[x-1][y]==0)){
            arr[x-1][y]=2;
            count++;
            dfs(arr,x-1,y);
            arr[x-1][y]=0;
            count--;
        }
    }
}

